// pages/shop-list/shop-list.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 0, //页数
    goods: [], //商品
    more: 0,
    class_id: '', //商品分类ID
    name:'',//分类名称
    scontent:"",//获取搜索内容
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function(options) {
    console.log(options);
    this.setData({
      class_id: options.class_id,
      name: options.name,
    });
    this.goodsList(options);

  },
  //商品列表
  goodsList: function(e) {
    // console.log(e.class_id);
    var that = this;
    // if (e.class_id){
    // that.setData({
    //   class_id: e.class_id,
    // });
    // }
    wx.request({
      url: app.globalData.baseUrl + "/a/mall/List/index",
      data: {
        limit: 10,
        page: that.data.page,
        id: that.data.class_id,
      },
      header: {
        // 'content-type': 'application',
        'TOKEN': "dxgANYXU4RqrAXo6OVwmBc9oodHWNXG2",
      },
      method: 'get',
      success(res) {
        console.log("商品0", res);
        if (res.data.code == 200) {

          wx.hideLoading();
          if (res.data.result.data) {
            if (that.data.more == 0) {
              that.setData({
                'goods': res.data.result.data,
              })
            } else {

              that.setData({
                'more': 0,
              })
              if (res.data.result.data.length > 0) {
                var info = that.data.goods.concat(res.data.result.data);
                that.setData({
                  'goods': info,
                  'more': 0,
                })
              }
            }
          }
        } else {

          wx.showToast({
            title: '已无更多...',
            icon: 'none',
            duration: 2000,

          })
        }
        // that.setData({
        //   goods:res.data.result.data,
        // });
        console.log("商品1", that.data.goods);
      }
    })
  },
  //获取搜索框输入内容
  search: function (e) {
    console.log("name", e.detail.value)
    this.setData({
      scontent: e.detail.value
    })
  },
  //搜索
  showsuo:function (){
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + "/a/mall/List/index",
      data: {
        keyword:that.data.scontent,
        limit: 10,
       
       
      },
      header: {
        'content-type': 'application/json',

        'TOKEN': "dxgANYXU4RqrAXo6OVwmBc9oodHWNXG2",
      },
      method: 'get',
      success(res) {
        console.log("---", res.data);
        if(res.data.code == 200){
          that.setData({
            goods: res.data.result.data,
          });
        }else{
          var scontent = that.data.scontent
          wx.showModal({
            title: '提示',
            content: '没有搜索到您要的内容:' + scontent + '（试例：桌子，' + '可以输入：桌）',
            
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
        
        // console.log("商品1", that.data.goods);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      'more': 0,
      'page': 1,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      'more': 1,
      'page': this.data.page + 1,
    });
    console.log('页码:', this.data.page)
    this.goodsList();

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})